package org.inspiration.dudu.user.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.*;
import org.inspiration.dudu.user.model.entity.UserFollowEntity;

import java.util.List;

@Mapper
public interface UserFollowMapper extends BaseMapper<UserFollowEntity> {
    /**
     * 查询某个用户关注的所有用户 ID 列表
     *
     * @param followerId 关注者的用户 ID
     * @return 被关注者的用户 ID 列表
     */
    @Select("SELECT following_id FROM user_follow WHERE follower_id = #{followerId} LIMIT #{offset}, #{pageSize}")
    List<Long> getFollowingUserIds(@Param("followerId") Long followerId, Integer offset, Integer pageSize);

    /**
     * 查询某个用户的所有粉丝的用户 ID 列表
     *
     * @param followingId 被关注者的用户 ID
     * @return 粉丝的用户 ID 列表
     */
    @Select("SELECT follower_id FROM user_follow WHERE following_id = #{followingId} LIMIT #{offset}, #{pageSize}")
    List<Long> getFollowerUserIds(@Param("followingId") Long followingId, Integer offset, Integer pageSize);

    /**
     * 判断一个用户是否关注了另一个用户
     *
     * @param followerId 关注者的用户 ID
     * @param followingId 被关注者的用户 ID
     * @return 如果关注返回 1，否则返回 0
     */
    @Select("SELECT COUNT(*) FROM user_follow WHERE follower_id = #{followerId} AND following_id = #{followingId}")
    int isFollowing(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

    /**
     * 插入关注关系
     *
     * @param followerId 关注者的用户 ID
     * @param followingId 被关注者的用户 ID
     */
    @Insert("INSERT INTO user_follow (follower_id, following_id) VALUES (#{followerId}, #{followingId})")
    void createFollow(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

    /**
     * 删除关注关系
     *
     * @param followerId 关注者的用户 ID
     * @param followingId 被关注者的用户 ID
     */
    @Delete("DELETE FROM user_follow WHERE follower_id = #{followerId} AND following_id = #{followingId}")
    void deleteFollow(@Param("followerId") Long followerId, @Param("followingId") Long followingId);

}
